package com.cweb.framework.generator.tableInfo.service;

import cn.hutool.core.util.StrUtil;
import com.cweb.framework.base.serivce.BaseMybatisService;
import com.cweb.framework.generator.gen.GeneratorUtil;
import com.cweb.framework.generator.tableInfo.entity.ColumnInfo;
import com.cweb.framework.generator.tableInfo.entity.TableInfo;
import com.cweb.framework.generator.tableInfo.mapper.TableInfoMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TableInfoService extends BaseMybatisService<TableInfoMapper, TableInfo> {
    /**
     * 表信息查询表信息
     */
    public List<TableInfo> selectTableList(TableInfo tableInfo){
        return this.getBaseMapper().selectTableList(tableInfo);
    }
    /**
     * 指定表名查询表信息
     */
    public TableInfo selectTableByName(String tableName) {
        return this.getBaseMapper().selectTableByName(tableName);
    }
    /**
     * 指定表名查询自动信息
     */
    public List<ColumnInfo> selectTableColumnsByName(String tableName) {
        return this.getBaseMapper().selectTableColumnsByName(tableName);
    }
    /**
     * 生成表对象
     */
    public void combineTableAndColumns(TableInfo table, List<ColumnInfo> columns) {
        // 表名转换成Java属性名
        String className = GeneratorUtil.tableToJava(table.getTableName());
        table.setClassName(className);
        table.setTableNameShort(table.getTableName().substring(table.getTableName().indexOf("_") + 1));
        table.setClassNameUpper(StrUtil.upperFirst(className));
        // 列信息
        table.setColumns(GeneratorUtil.transColums(columns));
        // 设置主键
        table.setPrimaryKey(table.getColumnsLast());
    }
}
